import 'dart:collection';

import 'package:aio/aio.dart';
import 'package:app/src/http/bean/wroks/contract_bean.dart';

///发货审核；通知单信息
class DeliveryNotice extends ContractCopyInfo {
  //结算额度
  String? Balanceamount = "";

  //当前欠款
  String? Currentdebt = "";

  //合计欠款
  String? Debt = "";

  //其他费用
  String? Otheramount = "";

  //发货面积
  String? CurrentArea = "";

  //发货金额
  String? Currentamount = "";

  //收货地址
  String? Toaddress = "";

  //收货人
  String? Towho = "";

  //联系电话
  String? Totelephone = "";

  //总车数
  String? CarNum = "";

  //是否包运费
  String? Isfreeshipping = "";

  //客户运费
  String? CustomerFreight = "";

  ///是否显示客户运费
  bool isShowCustomerFreight() {
    if ("否" == Isfreeshipping?.trim()) {
      return true;
    }
    return false;
  }

  //发货工厂
  String? DeliveryFactoryName = "";

  //是否跨厂拼车
  String? CarFactory = "";

  //跨厂拼车通知单
  String? NoticeList = "";

  //订单编号
  String? OrderCodes = "";

  //架卡编号
  String? Positioncodes = "";

  //备注
  String? Checkbeizhu = "";

  //额外收费说明
  List<AmountExplain>? AmountExplainList = null;

  @override
  fromJson(Map<String, dynamic> json) {
    super.fromJson(json);
    // TODO: implement fromJson
    Balanceamount = json['Balanceamount']?.toString() ?? "";
    Currentdebt = json['Currentdebt']?.toString() ?? "";
    Debt = json['Debt']?.toString() ?? "";
    Otheramount = json['Otheramount']?.toString() ?? "";
    CurrentArea = json['CurrentArea']?.toString() ?? "";
    Currentamount = json['Currentamount']?.toString() ?? "";
    Toaddress = json['Toaddress']?.toString() ?? "";
    Towho = json['Towho']?.toString() ?? "";
    Totelephone = json['Totelephone']?.toString() ?? "";
    CarNum = json['CarNum']?.toString() ?? "";
    Isfreeshipping = json['Isfreeshipping']?.toString() ?? "";
    CustomerFreight = json['CustomerFreight']?.toString() ?? "";
    DeliveryFactoryName = json['DeliveryFactoryName']?.toString() ?? "";
    CarFactory = json['CarFactory']?.toString() ?? "";
    NoticeList = json['NoticeList']?.toString() ?? "";
    OrderCodes = json['OrderCodes']?.toString() ?? "";
    Positioncodes = json['Positioncodes']?.toString() ?? "";
    Checkbeizhu = json['Checkbeizhu']?.toString() ?? "";
    AmountExplainList = setGenericFromJson(
        json, "AmountExplainList", <AmountExplain>[], AmountExplain());
    // json?.forEach((key, value) {
    //   print("key:$key\tvalue:\t$value");
    // });
  }

  @override
  instance() {
    // TODO: implement instance
    return DeliveryNotice();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = super.toJson();
    //var map = <String, dynamic>{};
    map["Balanceamount"] = Balanceamount;
    map["Currentdebt"] = Currentdebt;
    map["Debt"] = Debt;
    map["Otheramount"] = Otheramount;
    map["CurrentArea"] = CurrentArea;
    map["Currentamount"] = Currentamount;
    map["Toaddress"] = Toaddress;
    map["Towho"] = Towho;
    map["Totelephone"] = Totelephone;
    map["CarNum"] = CarNum;
    map["Isfreeshipping"] = Isfreeshipping;
    map["CustomerFreight"] = CustomerFreight;
    map["DeliveryFactoryName"] = DeliveryFactoryName;
    map["CarFactory"] = CarFactory;
    map["NoticeList"] = NoticeList;
    map["OrderCodes"] = OrderCodes;
    map["Positioncodes"] = Positioncodes;
    map["Checkbeizhu"] = Checkbeizhu;
    map["AmountExplainList"] = AmountExplainList;
    return map;
  }
}

///发货特殊收费说明（额外收费说明）
class AmountExplain extends BaseBean {
  //金额
  String? Acount = "";

  //单位
  String? Unit = "";

  //备注
  String? Intro = "";

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    Acount = json['Acount']?.toString() ?? "";
    Unit = json['Unit']?.toString() ?? "";
    Intro = json['Intro']?.toString() ?? "";
    // json?.forEach((key, value) {
    //   print("key:$key\tvalue:\t$value");
    // });
  }

  @override
  instance() {
    // TODO: implement instance
    return AmountExplain();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["Acount"] = Acount;
    map["Unit"] = Unit;
    map["Intro"] = Intro;
    return map;
  }
}

///发货单明细（1级）
class DeliveryList1 extends BaseBean {
  //2级明细
  List<DeliveryDetail2>? DeliveryDetails = null;

  //合计面积
  String? TArea = "";

  //合计数量
  String? TCount = "";

  //合计重量
  String? TWeight = "";

  //合计金额
  String? TMoney = "";

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    DeliveryDetails = setGenericFromJson(
        json, "DeliveryDetails", <DeliveryDetail2>[], DeliveryDetail2());
    TArea = json['TArea']?.toString() ?? "";
    TCount = json['TCount']?.toString() ?? "";
    TWeight = json['TWeight']?.toString() ?? "";
    TMoney = json['TMoney']?.toString() ?? "";
    // json?.forEach((key, value) {
    //   print("key:$key\tvalue:\t$value");
    // });
  }

  @override
  instance() {
    // TODO: implement instance
    return DeliveryList1();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["DeliveryDetails"] = DeliveryDetails;
    map["TArea"] = TArea;
    map["TCount"] = TCount;
    map["TWeight"] = TWeight;
    map["TMoney"] = TMoney;
    return map;
  }
}

///发货单明细（2级）
class DeliveryDetail2 extends BaseBean {
  //fixme 自己加的字段，判断二级列表是否打开。
  var isOpenAn = false;

  //订单编号
  String? OrderCode = "";

  //生产工厂
  String? FactoryName = "";

  //发货进度
  String? DeliveryProgress = "";

  //货品名称
  String? ProductName = "";

  //3级明细
  List<OrderDetail3>? OrderDetails = null;

  List<List<OrderDetail3>>? _OrderDetails2 = null;

  //相同的单号；进行分类。
  List<List<OrderDetail3>>? getOrderDetails2() {
    if (_OrderDetails2 == null) {
      _OrderDetails2 = [];
    } else if (_OrderDetails2!.length > 0) {
      return _OrderDetails2;
    }
    _OrderDetails2?.clear();
    var map = HashMap<String, List<OrderDetail3>>();
    OrderDetails?.forEach((element) {
      var key = element.CodeName;
      if (key != null) {
        if (map.containsKey(key)) {
          map[key]?.add(element);
        } else {
          var datas = <OrderDetail3>[];
          datas.add(element);
          map[key] = datas;
        }
      }
    });
    map?.forEach((key, value) {
      _OrderDetails2?.add(value);
    });
    return _OrderDetails2;
  }

  //小计面积
  String? DArea = "";

  //小计数量
  String? DCount = "";

  //小计重量
  String? DWeight = "";

  //小计金额
  String? DMoney = "";

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    OrderCode = json['OrderCode']?.toString() ?? "";
    FactoryName = json['FactoryName']?.toString() ?? "";
    DeliveryProgress = json['DeliveryProgress']?.toString() ?? "";
    ProductName = json['ProductName']?.toString() ?? "";
    OrderDetails = setGenericFromJson(
        json, "OrderDetails", <OrderDetail3>[], OrderDetail3());

    DArea = json['DArea']?.toString() ?? "";
    DCount = json['DCount']?.toString() ?? "";
    DWeight = json['DWeight']?.toString() ?? "";
    DMoney = json['DMoney']?.toString() ?? "";
    // json?.forEach((key, value) {
    //   print("key:$key\tvalue:\t$value");
    // });
  }

  @override
  instance() {
    // TODO: implement instance
    return DeliveryDetail2();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["OrderCode"] = OrderCode;
    map["FactoryName"] = FactoryName;
    map["DeliveryProgress"] = DeliveryProgress;
    map["ProductName"] = ProductName;
    map["OrderDetails"] = OrderDetails;

    map["DArea"] = DArea;
    map["DCount"] = DCount;
    map["DWeight"] = DWeight;
    map["DMoney"] = DMoney;
    return map;
  }
}

///发货单明细（3级）
class OrderDetail3 extends BaseBean {
  //单号
  String? CodeName = "";
  String _CodeName = "";

  ///获取单号，去除了单号里的中文字符，如：入库单号：J202012210021 -> J202012210021
  String getCodeName() {
    if (StringUtils.isEmpty(CodeName)) {
      return "";
    }
    if (StringUtils.isEmpty(_CodeName)) {
      _CodeName = StringUtils.replaceChinese(CodeName);
    }
    return _CodeName;
  }

  //类型
  String? Typename = "";

  //编号
  String? Itemcode = "";

  //规格型号
  String? Width_Height = "";

  //数量
  String? CanOutNum = "";

  //面积
  String? Area = "";

  //单价
  String? Price = "";

  //金额
  String? Amount = "";

  //架卡号
  String? ShelfCode = "";

  //箱号
  String? BoxOutCode = "";

  //优先级
  String? Priority = "";

  ///判断是否优先发货
  bool isPriority() {
    if (Priority != null && Priority!.contains("优先")) {
      return true;
    }
    return false;
  }

  //备注
  String? Listbz = "";

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    CodeName = json['CodeName']?.toString() ?? "";
    Typename = json['Typename']?.toString() ?? "";
    Itemcode = json['Itemcode']?.toString() ?? "";
    Width_Height = json['Width_Height']?.toString() ?? "";
    CanOutNum = json['CanOutNum']?.toString() ?? "";
    Area = json['Area']?.toString() ?? "";
    Price = json['Price']?.toString() ?? "";
    Amount = json['Amount']?.toString() ?? "";
    ShelfCode = json['ShelfCode']?.toString() ?? "";
    BoxOutCode = json['BoxOutCode']?.toString() ?? "";
    Priority = json['Priority']?.toString() ?? "";
    Listbz = json['Listbz']?.toString() ?? "";
    // json?.forEach((key, value) {
    //   print("key:$key\tvalue:\t$value");
    // });
  }

  @override
  instance() {
    // TODO: implement instance
    return OrderDetail3();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["CodeName"] = CodeName;
    map["Typename"] = Typename;
    map["Itemcode"] = Itemcode;
    map["Width_Height"] = Width_Height;
    map["CanOutNum"] = CanOutNum;
    map["Area"] = Area;
    map["Price"] = Price;
    map["Amount"] = Amount;
    map["ShelfCode"] = ShelfCode;
    map["BoxOutCode"] = BoxOutCode;
    map["Priority"] = Priority;
    map["Listbz"] = Listbz;
    return map;
  }
}
